package org.wishwork.api.auth.get;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.context.Context;
import org.wishwork.helper.JuShuiTanHelper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: WangYX
 * @create: 2022-04-01 17:05
 * @Version: 1.0.0
 **/

public class AuthUrl {

    private static final Logger log = LoggerFactory.getLogger(AuthUrl.class);

    private Context context;

    public AuthUrl(Context context) {
        this.context = context;
    }

    /**
     * 获取店铺授权的URL
     *
     * @param state
     * @return
     * @throws IOException
     */
    public String getAuthUrl(String state) throws Exception {
        Map<String, String> params = new HashMap<String, String>();
        // 公共参数
        params.put("app_key", context.properties().getAppKey());
        params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        if (StrUtil.isNotBlank(state)) {
            params.put("state", state);
        }
        params.put("charset", JuShuiTanHelper.DEFAULT_CHARSET);
        // 签名参数
        params.put("sign", JuShuiTanHelper.signTopRequest(params, context.properties().getAppSecret(), JuShuiTanHelper.SIGN_METHOD_MD5));
        String query = JuShuiTanHelper.buildQuery(params);
        return context.properties().getOpenWebUrl() + "/auth?" + query;
    }

}
